using System;
using System.Collections.Generic;
using System.Text;

using System.Reflection;
using System.Windows.Forms;

using Athena.Objects;

namespace Athena.Tools
{
    public class InsertGlyphTool : INonVisualTool
    {
        private static InsertGlyphTool _oInstance = new InsertGlyphTool();

        public override ToolManager.Order Order
        {
            get
            {
                return ToolManager.Order.FRONT;
            }
        }

        public static InsertGlyphTool Instance
        {
            get { return _oInstance; }
        }

        List<Type> _oDrawableTypes = new List<Type>();

        private InsertGlyphTool()
        {
            _oDrawableTypes = PluginManager.Instance.GetPlugins(typeof(IGlyph));

            // items to put into global tool window, if we found drawables
            _bHasMenuItems = _oDrawableTypes.Count > 0;
            // has items to put into context menu
            _bHasContextMenuItems = false;
            // no need a separate toolstrip for the arrange actions
            _bRequiresNewToolWindow = false;

            ToolManager.Instance.RegisterTool("Insert Tool", "Inserts new elements into the model", this);
            PluginManager.Instance.PluginsChanged += new PluginManager.PluginsChangedDelegate(Instance_PluginsChanged);
        }

        void Instance_PluginsChanged(object sender, EventArgs e)
        {
            _oDrawableTypes = PluginManager.Instance.GetPlugins(typeof(IGlyph));
        }




       
        public override void SetupToolStrip(System.Windows.Forms.ToolStrip oStrip)
        {
            _oStrip = oStrip;

            

            foreach (Type t in _oDrawableTypes)
            {
                try
                {
                    if (PluginManager.Instance.EnabledGlyphs.ContainsKey(t.Name) && (bool)(PluginManager.Instance.EnabledGlyphs[t.Name]))
                    {
                        IGlyph oPrototype = ((IGlyph)Activator.CreateInstance(t));
                        if (oPrototype.HasPreview)
                        {
                            ToolStripButton _oInsertGlyph = new ToolStripButton(oPrototype.GetPreview(20, 20));
                            _oInsertGlyph.ToolTipText = "Inserts new " + oPrototype.Type;
                            _oInsertGlyph.ImageScaling = ToolStripItemImageScaling.None;
                            _oInsertGlyph.Click += new EventHandler(OnInsert);
                            _oInsertGlyph.Tag = t;
                            oStrip.Items.Add(_oInsertGlyph);

                        }
                    }
                }
                catch (Exception)
                {

                }

            }
        }

        Type _oInsertType = null;


        

        void OnInsert(object sender, EventArgs e)
        {
            IsActive = true;
            if (sender is ToolStripButton)
            {
                ToolStripButton oButton = (ToolStripButton)sender;
                _oInsertType = (Type)oButton.Tag;
                UncheckAllButtons(oButton);                
                Utility.MouseImage = oButton.Image;
            }
        }

        public override void OnDeactivate()
        {
            UncheckAllButtons();
        }

        public void InsertNewAt(System.Drawing.PointF p)
        {
            if (!IsActive) return;
            if (_oInsertType == null) return;

            HistoryManager.History.SetControllPoint("Insert " + _oInsertType.Name);
            CurrentModel.DeselectItems(false);
            Utility.Refresh();
            IDrawable newInsert = (IDrawable)Activator.CreateInstance(_oInsertType, p);
            p.X -= newInsert.Dimensions.Width / 2;
            p.Y -= newInsert.Dimensions.Height / 2;
            newInsert.Location = p;
            CurrentModel.AddToModel(newInsert);
        }


        public override void OnKeyDown(System.Windows.Forms.KeyEventArgs args)
        {
            // based on keyboard shortcuts, perhaps insertion would be good
            base.OnKeyDown(args);
        }


        public override void OnMouseDown(HandledMouseEventArgs e)
        {
            System.Drawing.PointF location = Utility.GetTransformedPoint(e.Location);
            if (IsActive)
            {
                if (e.Button == MouseButtons.Left)
                {
                    InsertNewAt(location);
                    e.Handled = true;
                }
                //IsActive = false;
                
            }
            
        }


    }
}
